<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
   /*  function ajaxRequest(url,callback){
            let xhr = new XMLHttpRequest();
            xhr.open('GET',url,true);
            xhr.responseType='json'
            xhr.onload = function(){
                callback(null,xhr.response);
            }
            xhr.send();
    }
    //串行请求，解决回调地狱的问题
    ajaxRequest('data1.json',(err,data1)=>{
        ajaxRequest('data2.json',(err,data2)=>{
            ajaxRequest('data3.json',(err,data3)=>{
                console.log([data1,data2,data3]);
            });
        });
    }); */
    function ajaxRequest(url){
      return new Promise(function (resolve,reject){
            let xhr = new XMLHttpRequest();
            xhr.open('GET',url,true);
            xhr.responseType='json'
            xhr.onload = function(){
                resolve(xhr.response);
            }
            xhr.send();
      });
    }
    let result = [];
    ajaxRequest('data1.json')
    .then((data1)=>{
        result.push(data1);
        return ajaxRequest('data2.json');
    }).then((data2)=>{
        result.push(data2);
        return ajaxRequest('data3.json');
    }).then((data3)=>{
        result.push(data3);
        console.log(result);
    })
</script>
</body>
</html>